package com.ssmTest.service.impl;

import com.ssmTest.dao.IOrderDao;
import com.ssmTest.model.Order;
import com.ssmTest.service.IDownLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.util.List;

@Service("downLoadService")
public class DownLoadService implements IDownLoadService {
    @Resource
    private IOrderDao iOrderDao;
    @Autowired
    ReadExcel readExcel;
    @Autowired
    DateTimeServiceImpl dateTimeService;

    public int download(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        System.out.println("控制台输出：走入下载");
        //设置编码格式以及文件名称
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="
                + "date.xls");
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//设置参数的编码格式

        // String startDate = request.getParameter("startdate");//.replace('-', '.');
        String type = request.getParameter("enddate");//.replace('-', '.');


        int t = Integer.parseInt(type);
        String endDate = dateTimeService.getTime("yyyy.MM.dd");
        String startDate = "";
        if (t == 1) {
            startDate = endDate;
        } else if (t == 2) {
            System.out.println(type);
            startDate = dateTimeService.addday(endDate, -7);
            System.out.println(startDate);
        } else if (t == 3) {
            startDate = dateTimeService.addday(endDate, -30);
        } else if (t == 4) {
            startDate = dateTimeService.addday(endDate, -180);
        } else if (t == 5) {
            startDate = dateTimeService.addday(endDate, -365);
        } else if (t == 6) {
            startDate = dateTimeService.addday(endDate, -365 * 3);
        }

        List<Order> list = this.iOrderDao.selectOrderByDate(startDate, endDate);

        System.out.println("star" + startDate + "end" + endDate + "   " + list.size());
        //生成EXcel表
        readExcel.generaExcel(list);

        try {
            /*
             * String path = Thread.currentThread().getContextClassLoader()
			 * .getResource("").getPath() +
			 * "download";//这个download目录为啥建立在classes下的
			 */
            String path = "D:\\upload";
            InputStream inputStream = new FileInputStream(new File(path
                    + File.separator + "date.xls"));

            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }

            // 这里主要关闭。
            os.close();

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        // 返回值要注意，要不然就出现下面这句错误！
        // java+getOutputStream() has already been called for this response
        return 1;
    }
}
